#!/usr/bin/python

#$HeadURL$
#$Id$

from menu import *

import os
import sys
import shlex

def screen(command,title):
    os.system("screen -t %s %s" % (title,command))

def ssh(user_host):
    screen("ssh " + user_host, user_host)

# This doesn't seem the proper place for this.
# I feel more like calling m.dorestart()

def restart():
    m.dorestart()
    
def get_menu():
    lexer=shlex.shlex(file(os.environ['HOME'] + "/.sscreen" ))
    lexer.wordchars += '.@-_'
    menu_dict_list = [dict(txt=user_host,fun=ssh,args=(user_host,)) for user_host in lexer]
    menu_dict_list.extend(builtins)
    return menu_dict_list
def noop():
    pass

# * default 'new sscreen' menu option which opens a new screen
# running sscreen
# * default restart

builtins=(dict(txt="-----------",
               fun=noop,
               args=()
              ),
          dict(txt="new sscreen",
               fun=screen,
               args=(sys.argv[0],sys.argv[0])
              ),
          dict(txt="restart",
               fun=restart,
               args=()
              )
         )

m=menu(get_menu())

if __name__ == "__main__":

    if os.environ['TERM']== 'screen':
        m.dostart()
        while m.restart==1:
            m = menu(get_menu())
            m.dostart()
    else:
        os.system("screen -t %s python %s" % (sys.argv[0],sys.argv[0]))